import 'dart:async';

import 'package:equatable/equatable.dart';
import 'package:mobile_app.core/core/domain/config/config.dart';
import 'package:mobile_app.core/core/domain/repositories/analytics_repository/repository.dart';
import 'package:mobile_app.core/core/domain/services/analytics_service/service.dart';

part 'result.dart';

abstract class Initialize {
  InitializationResult call({
    required bool introStartLogged,
  });
}

class InitializeImpl implements Initialize {
  const InitializeImpl({
    required AnalyticsService analyticsService,
    required AnalyticsRepository analyticsRepository,
  })  : _analyticsService = analyticsService,
        _analyticsRepository = analyticsRepository;

  final AnalyticsService _analyticsService;
  final AnalyticsRepository _analyticsRepository;

  @override
  InitializationResult call({
    required bool introStartLogged,
  }) {
    var updatedIntroFinishLogged = introStartLogged;

    if (Config().analyticsCollectionEnabled && !introStartLogged) {
      _analyticsService.logIntroStart();

      unawaited(_analyticsRepository.saveIntroStartLogging(
        introStartLogged: true,
      ));

      updatedIntroFinishLogged = true;
    }

    final result = InitializationResult(
      introStartLogged: updatedIntroFinishLogged,
    );

    return result;
  }
}
